<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns="http://www.w3.org/2005/Atom"
                xmlns:atom="http://www.w3.org/2005/Atom"
                xmlns:a="http://www.w3.org/2005/Atom"
                xmlns:bc="http://brightcontent.net/ns/"
                xmlns:bce="http://brightcontent.net/ns/extensions"
                xmlns:xh="http://www.w3.org/1999/xhtml"
                xmlns:f="http://xmlns.4suite.org/ext"
                xmlns:func="http://exslt.org/functions"
                xmlns:regexp="http://exslt.org/regular-expressions"
                extension-element-prefixes="f func"
                exclude-result-prefixes="xh a atom f bce bc regexp #default">

 <xsl:param name="bc:base_url">http://localhost:8080</xsl:param>
 <xsl:param name="bc:collection_url">http://localhost:8080/atom/entries</xsl:param>
 <xsl:param name="bc:entries_base_url">http://localhost:8080/blog</xsl:param>

 <xsl:template match="/">
  <xsl:apply-templates />
 </xsl:template>

 <!-- adjust feed settings to create envelope file -->
 <xsl:template match="a:feed">
  <xsl:copy>
   <xsl:copy-of select="@*" />
   <xsl:apply-templates />
   <atom:id><xsl:value-of select="concat($bc:base_url, $bc:collection_url)" /></atom:id>
  </xsl:copy>
 </xsl:template>

 <!-- create our new entry -->
 <xsl:template match="a:entry">
  <xsl:variable name="entry-self" select="bce:create-self-link(./a:updated, bce:title-to-slug(./a:title))" />
  <xsl:copy>
   <atom:id><xsl:value-of select="concat($bc:base_url, $entry-self)" /></atom:id>
   <!-- add author info -->
   <xsl:if test="count(./a:author) = 0">
    <xsl:apply-templates select="../a:author" />
   </xsl:if>
   <xsl:copy-of select="@*" />
   <xsl:apply-templates />
   <atom:link title="permalink" rel="alternate" href="{concat($bc:base_url, bce:create-permalink(./a:updated, bce:title-to-slug(./a:title)))}" />
   <atom:link title="atom" rel="self" href="{concat($bc:base_url, $entry-self)}" />
   <atom:link title="edit" rel="edit" href="{concat($bc:base_url, $entry-self)}" />
  </xsl:copy>
 </xsl:template>

 <!-- element we don't want to copy -->
 <xsl:template match="a:link[@rel = 'self'] | a:link[@rel = 'alternate'] | a:link[@rel = 'edit'] | a:id" />
 <!-- invalid rel types we get rid of... b/c eric blew it and made one up at one point... -->
 <xsl:template match="a:link[@rel != 'alternate'] | a:link[@rel != 'enclosure'] | a:link[@rel != 'related'] | a:link[@rel != 'self'] | a:link[@rel != 'via']" />

 <!-- elements to copy blindly -->
 <xsl:template match="node()">
  <xsl:copy>
   <xsl:copy-of select="@*" />
   <xsl:apply-templates />
  </xsl:copy>
 </xsl:template>

 <!-- helpful functions -->
 <func:function name="bce:date-base-url">
  <xsl:param name="param-date" />
  <func:result select="f:strftime('/%Y/%m/%d', $param-date)" />
 </func:function>

 <func:function name="bce:entry-base-url">
  <xsl:param name="date" />
  <xsl:param name="slug" />
  <func:result select="concat(bce:date-base-url($date), concat('/', $slug))" />
 </func:function>

 <func:function name="bce:title-to-slug">
  <!-- SLUGCHARS = r'a-zA-Z0-9\-\_' -->
  <xsl:param name="title" />
  <func:result select="regexp:replace($title, '(\W)', 'g', '_')" />
 </func:function>

 <func:function name="bce:create-permalink">
  <xsl:param name="date" />
  <xsl:param name="slug" />
  <func:result select="concat(bce:entry-base-url($date, $slug), '/')" />
 </func:function>

 <func:function name="bce:create-self-link">
  <xsl:param name="date" />
  <xsl:param name="slug" />
  <func:result select="concat(bce:entry-base-url($date, $slug), '.atom')" />
 </func:function>

</xsl:stylesheet>